package com.qen.fx.custom;

import com.qen.App;
import com.qen.fx.ViewIndex;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.Cursor;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Border;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import lombok.Data;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ResourceBundle;

@Data
public class ContentHeader extends VBox implements Initializable {

    //    private HeaderCallback headerCallback;
    private String titleText;
    private String contentText;
    private Boolean isLike = false;
    private final InputStream nolike = App.class.getResourceAsStream("icon/nolike.png");
    private final InputStream like = App.class.getResourceAsStream("icon/like.png");
    @FXML
    private HBox header;
    @FXML
    private Pane pane;
    @FXML
    private Pane hBoxpPane;
    @FXML
    private Pane imagePane;
    @FXML
    private Label titleHeader;
    @FXML
    private Label contentLabel;
    @FXML
    private ImageView nolikeImage;
    @FXML
    private ImageView likeImage;
    @FXML
    private Separator separator;

//    public void setHeaderCallback(HeaderCallback headerCallback) {
//        this.headerCallback = headerCallback;
//    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

    }

    public void updateText() {
        titleHeader.setText(titleText);
        contentLabel.setText(contentText);
//        isLikeImage.setImage(new Image(isLike ? like : nolike));
        if (isLike) {
            likeImage.setVisible(true);
            likeImage.setManaged(true);
            nolikeImage.setVisible(false);
            nolikeImage.setManaged(false);
        } else {
            likeImage.setVisible(false);
            likeImage.setManaged(false);
            nolikeImage.setVisible(true);
            nolikeImage.setManaged(true);
        }
    }

    public ContentHeader() {
        FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(ViewIndex.CustomHeader));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ContentHeader controller = fxmlLoader.getController();
        this.prefHeight(ViewIndex.contentHeaderPreferHeight);
        this.prefWidth(ViewIndex.contentPreferWidth);
        this.maxHeight(-1);
        this.maxWidth(-1);
        this.minHeight(-1);
        this.minWidth(-1);

        header.setPrefWidth(ViewIndex.contentPreferWidth);
        header.setPrefHeight(80);

        hBoxpPane.setPrefHeight(80);
        hBoxpPane.prefWidth(ViewIndex.contentPreferWidth - 80);

        titleHeader.setPrefWidth(ViewIndex.contentPreferWidth - 200);
        controller.initContentHeader();
    }

    public void initContentHeader() {
        imagePane.setPrefHeight(80);
        imagePane.setPrefHeight(80);
        imagePane.setLayoutY(0);
        imagePane.setPadding(new Insets(20, 20, 20, 20));
//        titleHeader.setText("initContentHeader");
        titleHeader.setPadding(new Insets(15, 0, 0, 20));

        likeImage.setVisible(false);
        likeImage.setManaged(false);

        nolikeImage.setVisible(true);
        nolikeImage.setManaged(true);

        contentLabel.setText(contentText);
        contentLabel.setLayoutX(19);
        contentLabel.setLayoutY(-3);
        contentLabel.setPrefHeight(70);
        contentLabel.setPrefWidth(ViewIndex.contentPreferWidth - 200);
        pane.setPrefWidth(ViewIndex.contentPreferWidth);
        pane.setPrefHeight(70);
//        contentLabel.setText("使用您想要的字符、大写或小写字母、数字和/或符号生成随机字符串。使用您想要的字符、大写或小写字母、数字和/或符号生成随机字符串。");
        separator.setPrefWidth(ViewIndex.contentPreferWidth - 80);
    }

    public ContentHeader(String titleText, String contentText, boolean isLike) {
        super();
        this.prefHeight(ViewIndex.contentHeaderPreferHeight);
        this.prefWidth(ViewIndex.contentPreferWidth);
        this.maxHeight(-1);
        this.maxWidth(-1);
        this.minHeight(-1);
        this.minWidth(-1);

        Label label = new Label(titleText);
        label.setFont(new Font(40));
        Pane tPane = new Pane(label);
        tPane.prefHeight(80);
        tPane.prefWidth(ViewIndex.contentPreferWidth - 80);

        ImageView imageView = new ImageView();
        imageView.setFitHeight(40);
        imageView.setFitWidth(40);
        imageView.setCursor(Cursor.HAND);
        InputStream nolike = App.class.getResourceAsStream("icon/nolike.png");
        InputStream like = App.class.getResourceAsStream("icon/like.png");

        imageView.setImage(new Image(isLike ? like : nolike));
        imageView.setOnMouseClicked(e -> {
            imageView.setImage(new Image(isLike ? nolike : like));
        });
        Pane imagePane = new Pane(imageView);
        HBox hBox = new HBox(tPane, imagePane);

        Separator separator = new Separator();
        separator.setPrefWidth(100);
        separator.setPadding(new Insets(0, 0, 50, 50));

        Label conLabel = new Label(contentText);
        conLabel.setPrefHeight(70);
        conLabel.setPrefWidth(ViewIndex.contentPreferWidth);
        conLabel.setTextFill(Color.valueOf("#727272"));
        conLabel.setWrapText(true);
        conLabel.setLayoutX(19);
        conLabel.setLayoutY(-3);
        Pane pane = new Pane(conLabel);
        pane.setPrefHeight(70);
        pane.setPrefWidth(ViewIndex.contentPreferWidth);

        this.getChildren().addAll(hBox, separator, pane);
    }

    public String getTitleText() {
        return titleText;
    }

    public void setTitleText(String titleText) {
        this.titleText = titleText;
    }

    public String getContentText() {
        return contentText;
    }

    public void setContentText(String contentText) {
        this.contentText = contentText;
    }

    public Boolean getIsLike() {
        return isLike;
    }

    public void setIsLike(Boolean like) {
        isLike = like;
    }
}
